//
//  DataBaseManager.m
//  项目A --半糖
//
//  Created by romana on 16/6/2.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "DataBaseManager.h"
#import "SecondTabHotModel.h"

@implementation DataBaseManager

//数据库单例
+(DataBaseManager *)shareIndtance{

    static DataBaseManager *dbManager = nil;
    if (!dbManager) {
        dbManager = [[DataBaseManager alloc] init];
    }
    return dbManager;
}

//打开数据库
-(void)openHotDB{

    NSString *hotPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;

    NSLog(@"%@",hotPath);
    // 数据库路径
    NSString *hotFilePath = [hotPath stringByAppendingPathComponent:@"me"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:hotFilePath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:hotFilePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *dbHotNewPath = [NSString stringWithFormat:@"%@/me/hot.db",hotPath];
    self.mydb = [FMDatabase databaseWithPath:dbHotNewPath];
    BOOL r = [_mydb open];
    if (r) {
        NSLog(@"数据库打开成功");
    }else{
        NSLog(@"数据库打开失败");
    }
    
}
//执行语句封装
-(void)execSQLa:(NSString *)sql mes:(NSString *)mes{
    BOOL r = [_mydb executeUpdate:sql];
    if (r) {
        NSLog(@"%@ 成功",mes);
    }else{
        NSLog(@"%@ 失败",mes);
    }
}

// 创建表
-(void)createHotTable{
    NSString *creatSQL = @"CREATE TABLE IF NOT EXISTS hot (web TEXT ,imgView TEXT ,type TEXT ,title TEXT PRIMARY KEY)";
    [self execSQLa:creatSQL mes:@"创建hot表"];
}
-(void)dropHotTable{
    NSString *dropSQL = @"DROP TABLE hot";
    [self execSQLa:dropSQL mes:@"删除hot表"];
}

// 增
-(void)insertHotTitle:(NSString *)title imgView:(UIImage *)imgView WebView:(NSString *)web type:(NSString *)type{
    
    NSData *imagedata = UIImageJPEGRepresentation(imgView,1.0);
    
    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory=[paths objectAtIndex:0];
    //
    NSString *keyStr = [NSString stringWithFormat:@"me/hot%ld.jpg",(unsigned long)[title hash]];
    NSString *savedImagePath=[documentsDirectory stringByAppendingPathComponent:keyStr];
    
    [imagedata writeToFile:savedImagePath atomically:YES];
    NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO hot (title,imgView,web,type) VALUES('%@','%@','%@','%@')",title,keyStr,web,type];
    [self execSQLa:insertSQL mes:@"插入hot表"];
}
-(void)deleteHotByTitle:(NSString *)title{
    NSString *delectSQL = [NSString stringWithFormat:@"DELETE FROM hot WHERE title = '%@'",title];
    [self execSQLa:delectSQL mes:@"删除hot"];
}
// 查
-(BOOL)selectHotByTitle:(NSString *)title{
    NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM hot WHERE title = '%@'",title];
    FMResultSet *set = [_mydb executeQuery:selectSQL];
    BOOL r = [set next];
    return r;
}

//查询所有
-(NSMutableArray *)selectAllHot{
    NSMutableArray *mArr = [NSMutableArray array];
    NSString *selectSQL = @"SELECT * FROM hot";
    FMResultSet *set = [_mydb executeQuery:selectSQL];
    while ([set next]) {
        SecondTabHotModel *h = [[SecondTabHotModel alloc] init];
        h.name = [set stringForColumn:@"title"];
        h.url = [set stringForColumn:@"web"];
        h.cover_image_url = [set stringForColumn:@"imgView"];
        [mArr addObject:h];
        h.type = [set stringForColumn:@"type"];
    }
    return mArr;
}


@end
